// gameServer/demo/eventbus_demo.go
package main

import (
	"gameServer/cluster/gate"
	"gameServer/config"
	"gameServer/env"
	"gameServer/eventbus"
	"gameServer/log"
	"time"
)

func main6() {
	// 创建文件配置
	fileConfig := config.NewFileConfig("config.json")
	if err := fileConfig.Load(); err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	// 创建环境变量配置
	envConfig := env.NewEnvConfig("GAME")

	// 创建配置管理器
	manager := config.NewManager(envConfig, fileConfig)

	// 创建网关配置
	gateConfig := config.NewGateConfig()
	gateConfig.LoadFromManager(manager)

	log.Infof("Event bus config: type=%s, buffer_size=%d",
		gateConfig.EventBus.Type, gateConfig.EventBus.BufferSize)

	// 创建网关组件
	gateComponent := gate.NewGateWithConfig(gateConfig)

	// 订阅事件
	eventbus.Subscribe(eventbus.EventNodeRegistered, func(event eventbus.Event) {
		if nodeEvent, ok := event.(*eventbus.NodeEvent); ok {
			log.Infof("Node registered: %s (%s) at %s",
				nodeEvent.NodeID, nodeEvent.NodeType, nodeEvent.Address)
		}
	})

	// ... 其他订阅代码

	// 启动网关
	if err := gateComponent.Init(); err != nil {
		log.Fatalf("Failed to init gate: %v", err)
	}

	if err := gateComponent.Start(); err != nil {
		log.Fatalf("Failed to start gate: %v", err)
	}

	log.Info("Event bus demo started")

	// 运行一段时间
	time.Sleep(5 * time.Minute)

	// 停止网关
	if err := gateComponent.Stop(); err != nil {
		log.Errorf("Failed to stop gate: %v", err)
	}

	// 关闭事件总线
	eventbus.Close()

	log.Info("Event bus demo completed")
}
